/**
 * Created by jiax on 2018/11/6.
 */

export default {
    name: 'device',
    mounted: function () {
        this.findCompanyList();

        //保存下初始值
        this.defaultData = JSON.parse(JSON.stringify(this.addForm));

        this.resolutionTypes = this.$getDictList("device_resolution");
    },
    methods: {

        //分页size变化
        handleSizeChange: function(val) {
            this.pageInfo.pageSize = val;
            this.query(1);
        },
        //翻页
        handleCurrentChange: function(val) {
            this.query(parseInt(val));
        },
        // 分页查询
        query: function(pageNum) {
            this.loading = true;
            var params = {
                guid: this.searchGuid,
                companyId: this.companyId,
                resolution: this.type
            };
            this.axios.hPost('/api/device/findAll?pageIndex=' + pageNum + '&pageSize=' + this.pageInfo.pageSize, params, (response) => {
                this.deviceData = response.list;
                this.pageInfo.totalNum = response.totalCount;
                this.pageInfo.currentPage = pageNum;
                this.loading = false;
            });
        },

        findCompanyList: function() {
            this.axios.hGet('/api/company/findAll', {}, (response) => {
                this.companyList = response;
                this.query(1);
            });
        },

        transToMinute: function(spendTime) {
            if (!spendTime) {
                return 0;
            }
            return parseFloat(spendTime / 60).toFixed(2);
        },

        over: function($event) {
            $event.currentTarget.className='device-row el-row device-on';
        },

        out: function($event) {
            $event.currentTarget.className='device-row el-row';
        },

        getCompanyName: function(companyId) {
            if (!companyId) {
                return '';
            }

            if (!this.companyList) {
                return '';
            }

            var companyName = "";
            while (companyId != 0) {
                this.companyList.find(item => {
                    if (item.id == companyId) {
                        if (!companyName) {
                            companyName = item.companyName;
                        } else {
                            companyName = item.companyName + " - " + companyName;
                        }

                        companyId = item.parentId;
                        return false;
                    }
                });
            }
            return companyName;
        }

    },
    data: function() {
        return {

            //是否在加载标记
            loading: false,
            token: '',
            deviceData: [],
            noResult: false,
            loadingTxt: '',
            //存储分页数据
            pageInfo: {
                currentPage: 1,
                pageSize: 10,
                totalNum: 0
            },

            showDel: false,
            //搜索设备名称
            searchGuid: '',
            //搜索群组名称
            searchCompanyName: '',
            //表标题
            formTitle: '',
            //增加的表
            addForm: {
                id: null,
                guid: '',
                resolution: '',
                expireTime: null,
                companyId: null,
            },
            //默认数据
            defaultData: {},
            addFormVisible: false,
            infoVisible: false,
            saving: false,
            typeId: '',

            type: '',

            addRules: {
                name: [
                    {required: true, message: this.$t('message.required'), trigger: 'blur'},
                    {max: 50, message: this.$t('message.length50'), trigger: 'blur'},
                    {pattern: /^([\u4e00-\u9FFFa-zA-Z0-9+\s]+)$/, message: this.$t('message.incorrectFormat'), trigger: 'blur'}
                ],
                type: [
                    {required: true, message: this.$t('message.fieldSelected'), trigger: 'blur'}
                ]
            }
            ,
            companyId: "",
            resolutionTypes: [],
            companyList: []
        }
    }
}
